package com.sgb.member.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;

import com.sgb.member.BR;
import com.sgb.member.R;
import com.sgb.member.databinding.ItemMemberEnterpriseBinding;
import com.sgb.member.databinding.ItemMemberPersonBinding;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberPersonBean;
import com.sgb.member.weight.MemberCallback;
import com.sgb.member.weight.MemberCommonDialog;
import com.sgb.member.weight.MemberPersonCallback;
import com.sgb.member.weight.MemberTypeLayoutManager;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.StringUtil;

import java.util.List;

public class MemberCommonPersonAdapter extends BaseRecycleAdapter<MemberPersonBean> {

    private final int type;
    private final MemberCallback memberCallback;

    public MemberCommonPersonAdapter(Context context, List<MemberPersonBean> datas, int type, MemberCallback memberCallback) {
        super(context, R.layout.item_member_person, datas);
        this.type = type;
        this.memberCallback = memberCallback;
    }

    @Override
    public void convert(ItemViewHolder holder, final MemberPersonBean entity) {
        ItemMemberPersonBinding binding = (ItemMemberPersonBinding) holder.getmBinding();
        holder.setBinding(BR.viewModel, entity);
        binding.tvFollow.setVisibility(View.GONE);
        binding.ivMemberMore.setVisibility(View.VISIBLE);
        binding.tvPhone.setText(StringUtil.getNotEmptyStr(entity.userPhone));
        if (entity.allCategoryName != null && entity.allCategoryName.size() > 0) {
            binding.childRecyclerView.setVisibility(View.VISIBLE);
            binding.childRecyclerView.setLayoutManager(new MemberTypeLayoutManager(holder.itemView.getContext()));
            binding.childRecyclerView.setAdapter(new BaseRecycleAdapter<String>(holder.itemView.getContext(), R.layout.item_member_type_lable, entity.allCategoryName) {
                @Override
                public void convert(ItemViewHolder holder, String o) {
                    holder.setBinding(BR.itemBean, o);
                }
            });
        } else {
            binding.childRecyclerView.setVisibility(View.INVISIBLE);
        }

        StringBuilder sb = new StringBuilder();
        if (!StringUtil.isEmpty(entity.provinceName)) {
            sb.append(entity.provinceName).append("/");
        }
        if (!StringUtil.isEmpty(entity.cityName)) {
            sb.append(entity.cityName).append("/");
        }
        if (!StringUtil.isEmpty(entity.areaName)) {
            sb.append(entity.areaName).append("/");
        }
        if (!StringUtil.isEmpty(entity.detailAddress)) {
            sb.append(entity.detailAddress);
        }
        if (!StringUtil.isEmpty(sb.toString())) {
            binding.tvAddress.setText(sb.toString());
        } else {
            binding.tvAddress.setText("无");
        }
        if (!StringUtil.isEmpty(entity.personCompany)) {
            binding.tvPersonCompany.setText(entity.personCompany);
        } else {
            binding.tvPersonCompany.setText("无");
        }
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        ItemMemberPersonBinding binding = (ItemMemberPersonBinding) holder.getmBinding();
        binding.tvModifyMemberType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        binding.tvModifyMemberLever.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1供方会员 2需方会员
                MemberPersonBean memberPersonBean = datas.get(position);
                MemberBean memberBean = new MemberBean();
                memberBean.setNo(memberPersonBean.no);
                memberBean.setLevel(memberPersonBean.userLevel);
                memberBean.setCompanyName(memberPersonBean.userName);
                memberBean.setCompanyNo(memberPersonBean.userNo);
                memberBean.setCheckType(1);
                // 1供方会员 2需方会员
                if (type == 0) {
                    memberBean.setMemberUpdateLeverType("2");
                } else if (type == 1) {
                    memberBean.setMemberUpdateLeverType("1");
                }
                if (memberCallback != null) {
                    memberCallback.onMemberTransfer(position, memberBean);
                }
            }
        });
        //解除会员
        binding.tvMemberDissolution.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1供方会员 2需方会员
                MemberPersonBean memberPersonBean = datas.get(position);
                MemberBean memberBean = new MemberBean();
                memberBean.setNo(memberPersonBean.no);
                memberBean.setCompanyName(memberPersonBean.userName);
                memberBean.setCompanyNo(memberPersonBean.userNo);
                memberBean.setCheckType(1);
                if (memberCallback != null) {
                    memberCallback.onMemberDissolution(position, memberBean);
                }
            }
        });
//        注释关注操作
//        binding.tvFollow.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showDialog(v.getContext(), position, datas.get(position));
//            }
//        });
    }

    public void showDialog(Context context, int pos, MemberBean bean) {
        MemberCommonDialog dialog = new MemberCommonDialog(new MemberCommonDialog.SimpleListener() {
            @Override
            public void onPositive(Object... objects) {
                if (listener != null) {
                    listener.onFollow(pos, bean);
                }
            }
        }, MemberCommonDialog.STYLE_FOLLOW);
        dialog.show(context, "MemberCommonDialog");
    }

    private IFollowListener listener;

    public void setListener(IFollowListener listener) {
        this.listener = listener;
    }

    public interface IFollowListener {
        void onFollow(int pos, MemberBean bean);
    }
}
